package cn.iocoder.yudao.module.wine.controller.admin.supplycount;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.wine.controller.admin.supplycount.vo.*;
import cn.iocoder.yudao.module.wine.dal.dataobject.supplycount.SupplyCountDO;
import cn.iocoder.yudao.module.wine.service.supplycount.SupplyCountService;

@Tag(name = "管理后台 - 酒类行情日K")
@RestController
@RequestMapping("/wine/supply-count")
@Validated
public class SupplyCountController {

    @Resource
    private SupplyCountService supplyCountService;

    @PostMapping("/create")
    @Operation(summary = "创建酒类行情日K")
    @PreAuthorize("@ss.hasPermission('wine:supply-count:create')")
    public CommonResult<Long> createSupplyCount(@Valid @RequestBody SupplyCountSaveReqVO createReqVO) {
        return success(supplyCountService.createSupplyCount(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新酒类行情日K")
    @PreAuthorize("@ss.hasPermission('wine:supply-count:update')")
    public CommonResult<Boolean> updateSupplyCount(@Valid @RequestBody SupplyCountSaveReqVO updateReqVO) {
        supplyCountService.updateSupplyCount(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除酒类行情日K")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('wine:supply-count:delete')")
    public CommonResult<Boolean> deleteSupplyCount(@RequestParam("id") Long id) {
        supplyCountService.deleteSupplyCount(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得酒类行情日K")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('wine:supply-count:query')")
    public CommonResult<SupplyCountRespVO> getSupplyCount(@RequestParam("id") Long id) {
        SupplyCountDO supplyCount = supplyCountService.getSupplyCount(id);
        return success(BeanUtils.toBean(supplyCount, SupplyCountRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得酒类行情日K分页")
    @PreAuthorize("@ss.hasPermission('wine:supply-count:query')")
    public CommonResult<PageResult<SupplyCountRespVO>> getSupplyCountPage(@Valid SupplyCountPageReqVO pageReqVO) {
        PageResult<SupplyCountDO> pageResult = supplyCountService.getSupplyCountPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SupplyCountRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出酒类行情日K Excel")
    @PreAuthorize("@ss.hasPermission('wine:supply-count:export')")
    @OperateLog(type = EXPORT)
    public void exportSupplyCountExcel(@Valid SupplyCountPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SupplyCountDO> list = supplyCountService.getSupplyCountPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "酒类行情日K.xls", "数据", SupplyCountRespVO.class,
                        BeanUtils.toBean(list, SupplyCountRespVO.class));
    }

}